"use strict";
/* AXIOS的二次配置 */
const http = axios.create({
	baseURL: 'http://127.0.0.1:9999',
	timeout: 60000,
	withCredentials: true,
	// 我研发的后台有要求：POST请求中，基于请求主体传递给服务器的数据格式，应该是URL-ENCODED格式字符串 
	transformRequest: data => {
		if (utils.isPlainObject(data)) return Qs.stringify(data);
		return data;
	},
	validateStatus: status => {
		return status >= 200 && status < 400;
	}
});
// 请求拦截器:统一把本地存储的Token发送给服务器
http.interceptors.request.use(config => {
	let { url, method } = config,
		token = utils.storage.get('tk');
	if (token && !(url === '/user/login' && method.toLowerCase() === 'post')) {
		config.headers['authorzation'] = token;
	}
	return config;
});
// 响应拦截器
http.interceptors.response.use(response => {
	return response.data;
}, reason => {
	alert('小主,当前网络繁忙,请稍后再试!');
	return Promise.reject(reason);
});

/* 公共的接口请求 */
const api = {
	async queryDepartmentList() {
		let departmentList = utils.storage.get('departmentList', 24 * 60 * 60 * 1000);
		if (departmentList) return departmentList;
		// 本地缓存没有，则从服务器获取
		departmentList = [];
		try {
			let { code, data } = await http.get('/department/list');
			if (+code === 0) departmentList = data;
		} catch (_) { }
		utils.storage.set('departmentList', departmentList);
		return departmentList;
	},
	async queryJobList() {
		let jobList = utils.storage.get('jobList', 24 * 60 * 60 * 1000);
		if (jobList) return jobList;
		jobList = [];
		try {
			let { code, data } = await http.get('/job/list');
			if (+code === 0) jobList = data;
		} catch (_) { }
		utils.storage.set('jobList', jobList);
		return jobList;
	}
};